package com.clive.xxweb.service.impl;

import com.clive.xxweb.bean.Dish;
import com.clive.xxweb.exception.GlobalException;
import com.clive.xxweb.mapper.DishCategoryMapper;
import com.clive.xxweb.mapper.DishMapper;
import com.clive.xxweb.service.DishService;
import com.clive.xxweb.vo.DishVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishCategoryMapper dishCategoryMapper;
    @Override
    public int addDish(Dish dish) {
        if(dish.getCategory().getId()==null){
            throw new GlobalException("菜品分类不能为空");
        }
        Date date = new Date();
        dish.setStatus(1);
        dish.setCreated(date);
        dish.setUpdated(date);
        dish.setBuyCount(0);
        int i = dishMapper.addDish(dish);
        if (i<=0){
           throw new GlobalException("菜品分类不能为空");
        }
        return i;
    }
    @Override
    public Dish findDishOne(Integer id) {
        Dish dish = dishMapper.findOne(id);
        return dish;
    }

    @Override
    public List<Dish> findAll() {
        List<Dish> list = dishMapper.findAll();
        return list;
    }

    @Override
    public int updateDish(Dish dish) {
        if(dish.getCategory().getId()==null){
            throw new GlobalException("菜品分类不能为空");
        }
        Date date = new Date();
        dish.setUpdated(date);
        int i= dishMapper.updateDish(dish);
        if(i<=0){
            throw new GlobalException("修改菜品失败");
        }
       return i;
    }

    @Override
    public int deleteDish(Integer id) {
        int i = dishMapper.deleteDish(id);
        if (i<=0) {
            throw new GlobalException("删除失败");
        }
        return i;
    }


    @Override
    public DishVo findqueryDish(Integer currentPage, Integer limit,Dish dish) {
        DishVo vo = new DishVo();
        List<Dish> list = dishMapper.findCurrentPageDish((currentPage-1)*limit,limit,dish);
        vo.setDishess(list);
        int dishesTotal = dishMapper.finddishTotal(dish);
        vo.setDishestotal(dishesTotal);
        return vo;
    }


}
